package moodish.test;

import java.io.Serializable;

import junit.framework.Assert;
import moodish.comm.ServerSideMessage;
import moodish.comm.ServerSideMessage.Type;
import moodish.dummy.comm.SSM;

import org.junit.Test;

public class SSMTestCase {

		private final static Type DEF_TYPE = Type.CLIENT_CONNECTED;
		private final static String DEF_SENDER_NICKNAME = "Sender Nickname";
		private final static String DEF_PAYLOAD = "Message Payload";
		
		@Test
		public void constructorTest1() {
			SSM serverSideMessage = new SSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertNotNull(serverSideMessage);
		}

		@Test(expected=IllegalArgumentException.class)
		public void constructorTest2() {
			SSM serverSideMessage = new SSM(null, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertNotNull(serverSideMessage);
		}

		@Test(expected=IllegalArgumentException.class)
		public void constructorTest3() {
			SSM serverSideMessage = new SSM(DEF_TYPE, null, DEF_PAYLOAD);
			Assert.assertNotNull(serverSideMessage);
		}

		@Test
		public void getTypeTest() {
			Type[] typesArray = Type.values();
			for (Type type : typesArray) {
				SSM serverSideMessage = new SSM(type, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
				Assert.assertNotNull(serverSideMessage);
				Assert.assertEquals(type, serverSideMessage.getType());
			}
		}

		@Test
		public void getSendersNicknameTest() {
			SSM serverSideMessage = new SSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertNotNull(serverSideMessage);
			Assert.assertEquals(DEF_SENDER_NICKNAME, serverSideMessage.getClientNickname());
		}
		
		@Test
		public void getPayloadTest1() {
			SSM serverSideMessage = new SSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertNotNull(serverSideMessage);
			Assert.assertEquals(DEF_PAYLOAD, serverSideMessage.getPayload());
		}
		
		@Test
		public void getPayloadTest2() {
			SSM serverSideMessage = new SSM(DEF_TYPE, DEF_SENDER_NICKNAME, null);
			Assert.assertNotNull(serverSideMessage);
			Assert.assertNull(serverSideMessage.getPayload());
		}
		
		@Test
		public void toStringTest1() {
			Type[] typesArray = Type.values();
			for (Type type : typesArray) {
				String expectedToString = "[" + type.toString() + "] - Sender:" + DEF_SENDER_NICKNAME + ", Payload: " + DEF_PAYLOAD + "."; 
				SSM serverSideMessage = new SSM(type, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
				Assert.assertNotNull(serverSideMessage);
				Assert.assertEquals(expectedToString, serverSideMessage.toString());
			}
		}
		
		@Test
		public void toStringTest2() {
			Type[] typesArray = Type.values();
			
			for (Type type : typesArray) {
				String expectedToString = "[" + type.toString() + "] - Sender:" + DEF_SENDER_NICKNAME + "."; 
				SSM serverSideMessage = new SSM(type, DEF_SENDER_NICKNAME, null);
				Assert.assertNotNull(serverSideMessage);
				Assert.assertEquals(expectedToString, serverSideMessage.toString());
			}
		}
		
		@Test
		public void isInstanceOfServerSideMessage() {
			SSM serverSideMessage = new SSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertTrue(serverSideMessage instanceof ServerSideMessage);
		}
		
		@Test
		public void isInstanceOfSerializable() {
			SSM serverSideMessage = new SSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertTrue(serverSideMessage instanceof Serializable);
		}
	}
